package com.reform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.reform.pojo.project.ProjectTeaching;
import com.reform.mapper.ProjectTeachingMapper;
import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IProjectTeachingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reform.utils.VerifycUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 主要教学工作简历 服务实现类
 * </p>
 *
 * @author mortale
 * @since 2021-07-20
 */
@Service
public class ProjectTeachingServiceImpl extends ServiceImpl<ProjectTeachingMapper, ProjectTeaching> implements IProjectTeachingService {

    @Autowired
    ProjectTeachingMapper projectTeachingMapper;



    @Override
    public Integer deleteProjectTeachingByProId(String proId) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        map.put("pro_id", proId);
        return projectTeachingMapper.deleteByMap(map);
    }


    @Override
    public Integer creatProjectTeachingList(List<ProjectTeaching> projectTeachingList) throws SQLException {
        int res = 0;
        for (ProjectTeaching projectTeaching : projectTeachingList) {
            res += projectTeachingMapper.insert(projectTeaching);
        }
        if (res != projectTeachingList.size()) {
            return -1;
        }
        return res;
    }

    @Override
    public Integer updateProjectTeachingList(List<ProjectTeaching> projectTeachingList) throws SQLException {
        int res = 0;
        for (ProjectTeaching projectTeaching : projectTeachingList) {
            projectTeachingMapper.updateById(projectTeaching);
            res++;
        }
        return res;
    }

    @Override
    public Result deleteProjectTeachingByTeachingId(String teaching_id) throws SQLException {
        if (projectTeachingMapper.deleteById(teaching_id) > 0)
            return Result.success(ResultCode.SUCCESS);
        return Result.failure(ResultCode.FAILURE);
    }

    @Override
    public Result insertProjectTeaching(String projectTeachings) throws SQLException {
        List<ProjectTeaching> projectTeachingList;
        try {
            projectTeachingList = JSON.parseArray(projectTeachings, ProjectTeaching.class);
        } catch (JSONException e) {
            return Result.failure(ResultCode.PARAM_JSON_IS_WRONG, e.getMessage());
        }
        int res = 0;
        for (ProjectTeaching projectTeaching : projectTeachingList) {
            if (!VerifycUtils.verifyNullOrEmptyUtils(projectTeaching.getProId())) {
                return Result.failure(ResultCode.PARAM_IS_MISSING);
            }
        }
        for (ProjectTeaching projectTeaching : projectTeachingList) {
            res += projectTeachingMapper.insert(projectTeaching);
        }
        if (res != projectTeachingList.size()) {
            return Result.failure(ResultCode.FAILURE);
        }
        return Result.success(ResultCode.SUCCESS);
    }
}
